﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WMSSyncData.Model.WMSModel;
using WMSSyncData.Utils;

namespace WMSSyncData.DAL.SqlDB
{
    public class WMS_T_InStorckRMRowDetailsDAL : BaseSqlDBDAL<WMS_T_InstockRMRowDetails>
    {
        public List<WMS_T_InstockRMRowDetails> GetAllUpdate()
        {
            string sql = @"SELECT * FROM dbo.wms_T_InStockRMRows 
WHERE BillNo IN (SELECT BillNo FROM dbo.wms_T_InStockRM WHERE Status='1' and BillType<>'退料入库' and BillType<>'进口采购退货' and BillType<>'非拉动退料入库')";
            var dt = SqlDBHelper.GetDataSet(sql).Tables[0];

            var result = MakeTablePackage(dt);
            return result;

        }

        public List<WMS_T_InstockRMRowDetails> GetPRUpdate()
        {
            string sql = @"SELECT * FROM dbo.wms_T_InStockRMRows 
WHERE BillNo IN (SELECT BillNo FROM dbo.wms_T_InStockRM WHERE Status='6' and BillType='退料入库')";
            var dt = SqlDBHelper.GetDataSet(sql).Tables[0];

            var result = MakeTablePackage(dt);
            return result;

        }

        public List<WMS_T_InstockRMRowDetails> GetPRBOMUpdate()
        {
            string sql = @"SELECT * FROM dbo.wms_T_InStockRMRows 
WHERE BillNo IN (SELECT BillNo FROM dbo.wms_T_InStockRM WHERE Status='6' and BillType='非拉动退料入库')";
            var dt = SqlDBHelper.GetDataSet(sql).Tables[0];

            var result = MakeTablePackage(dt);
            return result;

        }

        public int UpdateMdcDatVStorage(DataTable dt)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("begin");
            foreach (DataRow dr in dt.Rows)
            {
                var sql = "UPDATE MdcDatVStorage set  qty='"+dr["PLANQTY"].ToString() + "' where ReelID='" + dr["UniqueCode"].ToString() + "';";
                sb.AppendLine(sql);
            }
            sb.AppendLine("end;");

            var resultSql = sb.ToString().Trim();
            var result = SqlDBHelper.ExcuteSQL(resultSql);
            return result;
        }

        public DataTable GetInventoryInfoByConditon(string PartNumber,string Version,string DateCode)
        {
            var sql = @"select  * from  wms_T_Inventory where PartNumber='"+ PartNumber + "' and Version='"+ Version + "' and DateCode='"+ DateCode + "';";
            var dt = SqlDBHelper.GetDataSet(sql).Tables[0];
            return dt;
        }
    }
}
